php delete element from array

49

php delete item from array -

if (in_array('strawberry', $array)) 
{
    unset($array[array_search('strawberry',$array)]);
}

Deleting an element from an array in PHP -

$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]); //Key which you want to delete
/*
$array:
[
    [0] => a
    [2] => c
]
*/
//OR
$array = [0 => "a", 1 => "b", 2 => "c"];
array_splice($array, 1, 1);//Offset which you want to delet
/*
$array:
[
    [0] => a
    [1] => c
]
*/

Comments

Submit
0 Comments